package com.example.study.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.study.mapper.UserMapper;
import com.example.study.pojo.User;
import com.example.study.service.UserService;
import jdk.internal.org.objectweb.asm.Handle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

@Controller
public class TestController {
    @Autowired
    private UserService userService;

    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        User byId = userService.getById("181210204");
        return byId.toString();
    }

    @RequestMapping("/test1")
    @ResponseBody
    public String test1() {
        List<User> users = userService.selctAllUser();
        return users.toString();
    }

    @RequestMapping("/test2")
    @ResponseBody
    public int test2() {
        //条件构造器
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", "云深不知处")
                .between("reward", 5, 100);

        int count = userService.count(userQueryWrapper);
        return count;
    }

    //插入操作
    @RequestMapping("/test3")
    @ResponseBody
    public String test3() {
        User user = new User();
        user.setBackground("hello");
        user.setCollect("hello");
        user.setPassword("hwello");
        user.setReward(1);
        User user2 = new User();
        user2.setBackground("hello");
        user2.setCollect("hello");
        user2.setPassword("hwello");
        user2.setReward(1);
        List<User> list = new ArrayList<>();
        list.add(user);
        list.add(user2);
        //条件构造器
//        boolean save = userService.save(user);
        //插入多条数据
        boolean b = userService.saveBatch(list);
        return b ? "success" : "failed";
    }

    //like模糊查询
    @RequestMapping("/test4")
    @ResponseBody
    public String test4() {
        Page<User> userPage = new Page<>(2, 5);
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.notLike("username", "a")
                .likeRight("username", "喜欢")
                .ge("reward", 5)
                .orderByDesc("reward");
        IPage<User> page = userService.page(userPage, userQueryWrapper);
        int count = userService.count(userQueryWrapper);
        List<User> records = page.getRecords();
        return records.toString() + "=============>" + String.valueOf(count);
    }

    //
    //like模糊查询
    @RequestMapping("/test5")
    @ResponseBody
    public String test5() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("reward", 5);
        params.put("username", "喜欢多云的季节");
        List<User> users = (List<User>) userService.listByMap(params);
        return users.toString();
    }

    //like模糊查询
    @RequestMapping("/test6")
    @ResponseBody
    public String test6() {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        HashMap<String, Object> params = new HashMap<>();
        params.put("collect", "hello");
        userQueryWrapper.allEq(params);

        boolean remove = userService.remove(userQueryWrapper);
        return remove ? "成功" : "失败";
    }

    //
    //like模糊查询
    @RequestMapping("/test7")
    @ResponseBody
    public String test7() {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();

//        userQueryWrapper.inSql("reward","5,15,10");
        //可以通过外键进行表的关联
        userQueryWrapper.inSql("reward", "select reward from user where reward>5");
        String sqlSet = userQueryWrapper.getSqlSet();
        List<User> list = userService.list(userQueryWrapper);
        int count = userService.count(userQueryWrapper);
        return list.toString() + "============>" + String.valueOf(count) + "======>" + sqlSet;
    }

    //like模糊查询
    @RequestMapping("/test8")
    @ResponseBody
    public boolean test8() {
        User user = new User();
        UpdateWrapper<User> userQueryWrapper = new UpdateWrapper<>();

//        userQueryWrapper.inSql("reward","5,15,10");
        //可以通过外键进行表的关联
        userQueryWrapper.eq("account","181210205")
                .set("email","3193920282@qq.com");
        boolean list = userService.update(user, userQueryWrapper);

        int count = userService.count(userQueryWrapper);
        return list;
    }
}
